from PIL import Image
from selenium import webdriver
import time
import pytesseract


def test1():

    # 打开浏览器,并最大化
    driver = webdriver.Chrome()
    driver.get('http://localhost:8080/jpress/user/register')
    driver.maximize_window()

    # 截屏
    screen_name = 'jp.png'
    driver.save_screenshot(screen_name)

    # 定位坐标，找出验证码图片
    kt = driver.find_element_by_id('captchaimg')
    print(kt.location)
    left = kt.location['x']
    top = kt.location['y']
    right = left + kt.size['width']
    lower = top + kt.size['height']

    # PIL模块的应用，抠图
    im = Image.open(screen_name)
    img = im.crop((left,top,right,lower))
    img.save('yzm.png')
    driver.close()


def test2():
    picture = Image.open('test1.jpg')
    # 调用pytesseract, 识别图片
    str = pytesseract.image_to_string(picture)
    print(str)